﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace UFrame
{
    public class PureMouseSelectGameObject : MonoBehaviour
    {
        private Ray ray;
        private RaycastHit hit;
        private GameObject selectObj;
        public bool isDebug;

        public int layerMask;

        private List<GameObject> worldObjList;
        public Material[] gameObjectMaterial;

        public Shader selectShader;
        public Shader customShader;

        public Vector2 mouseStartPosition = new Vector2(0, 0);
        public Vector2 mouseEndPosition = new Vector2(0, 0);
        public Vector2 mousePosition = new Vector2(0, 0);

        public bool mouseUp = false;
        public bool mouseDrag = false;
        public bool mouseDown = false;

        public Material drawMat;

        //其他行为
        private bool isMouseAction = false;

        private void Awake()
        {
            layerMask = 1 << LayerMask.NameToLayer("Player") |
                        1 << LayerMask.NameToLayer("Terrain") |
                        1 << LayerMask.NameToLayer("Construction") |
                        1 << LayerMask.NameToLayer("UI");
            //1 << LayerMask.NameToLayer("MouseNoClick");

            //EventMa.Register(MouseSelectTargetArgs.EventId, OnMouseSelectTargetAction);
        }

        private void OnMouseSelectTargetAction(params object[] oList)
        {
            isMouseAction = true;
            //MouseSelectTargetArgs m = oList[0] as MouseSelectTargetArgs;
        }

        void Start()
        {
            //gameObjectSelected = new bool[gameObjects.Length];
            //gameObjectMaterial = new Material[gameObjects.Length];
            //for (int i = 0; i < gameObjects.Length; i++)
            //{
            //    gameObjectMaterial[i] = gameObjects[i].GetComponent<MeshRenderer>().material;
            //}

            drawMat.hideFlags = HideFlags.HideAndDontSave;
            drawMat.shader.hideFlags = HideFlags.HideAndDontSave;
        }

        // Update is called once per frame
        void Update()
        {
            mousePosition = Input.mousePosition;
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                mouseDown = true;
                mouseStartPosition = Input.mousePosition;
                //JiangShiWorldMB.I.NoAllSelect();
            }

            if (Input.GetKeyUp(KeyCode.Mouse0))
            {
                mouseEndPosition = Input.mousePosition;
                mouseUp = true;
                mouseDown = false;

                IsSelect();
            }

            if (Input.GetKeyUp(KeyCode.Mouse1))
            {
                //JiangShiWorldMB.I.NoAllSelect();
            }

            if (Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKeyUp(KeyCode.Mouse1))
            {
                ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 500, layerMask))
                {
                    bool isLeft = Input.GetKeyUp(KeyCode.Mouse0);
                    if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
                    {
                        //Debug.Log("点击的是UI");
                        return;
                    }

                    selectObj = hit.collider.gameObject;
                    //BaseObj bc = selectObj.GetComponentInParent<BaseObj>();
                    //EventMa.Notify(ClickGameObjArgs.EventId, selectObj, isLeft);

                    //if (isLeft)
                    //{
                    //    JiangShiWorldMB.I.SelectObj(selectObj);
                    //    EventMa.Notify(ClickGameObjArgs.EventId, selectObj, isLeft);
                    //}
                    //else
                    //{
                    //    //右键
                    //    if (selectObj.layer == LayerMask.NameToLayer("Terrain"))
                    //    {
                    //        JiangShiWorldMB.I.Move(hit.point);

                    //    }
                    //    else
                    //    {
                    //        if (JiangShiWorldMB.I.selectObjList.Count > 0)
                    //        {
                    //            JiangShiWorldMB.I.SetTarget(selectObj);

                    //        }
                    //        else
                    //        {
                    //            JiangShiWorldMB.I.SelectObj(selectObj);

                    //        }
                    //    }
                    //}


                    if (isDebug)
                    {
                        Debug.Log(selectObj.name);
                    }
                }
                else
                {
                    if (isDebug)
                    {
                        Debug.Log("没有选择任何物体");
                    }
                }
            }
        }

        private void IsSelect()
        {
            //if (mouseStartPosition != mouseEndPosition)
            //{
            //    for (int i = 0; i < JiangShiWorldMB.I.worldObjList.Count; i++)
            //    {
            //        GameObject obj = null;
            //        Vector2 po = Camera.main.WorldToScreenPoint(obj.transform.position);
            //        Vector2 ms = mouseStartPosition;
            //        Vector2 me = mouseEndPosition;
            //        if ((po.x > ms.x && po.x < me.x && po.y > ms.y && po.y < me.y) ||
            //            (po.x > ms.x && po.x < me.x && po.y < ms.y && po.y > me.y) ||
            //            (po.x < ms.x && po.x > me.x && po.y > ms.y && po.y < me.y) ||
            //            (po.x < ms.x && po.x > me.x && po.y < ms.y && po.y > me.y))
            //        {
            //            //gameObjects[i].GetComponent<MeshRenderer>().material.shader = selectShader;
            //            //gameObjects[i].GetComponent<single>().isSelected = true;
            //            //JiangShiWorldMB.I.SelectObj(obj);
            //        }
            //    }
            //}
            //else
            //{


            //}
        }

        private void OnGUI()
        {
            if (mouseDown)
            {
                Draw();
            }
        }

        private void Draw()
        {
            if (drawMat == null)
            {
                return;
            }

            drawMat.SetPass(2);
            GL.LoadOrtho();
            GL.PushMatrix(); //保存摄像机变换矩阵  
            GL.LoadPixelMatrix(); //设置用屏幕坐标绘图  
            //透明框  
            Color clr = Color.yellow;

            GL.Begin(GL.LINES);
            GL.Color(clr);
            GL.Vertex3(mouseStartPosition.x, mouseStartPosition.y, 0);
            GL.Vertex3(mousePosition.x, mouseStartPosition.y, 0);
            GL.End();

            //下  
            GL.Begin(GL.LINES);
            GL.Color(clr);
            GL.Vertex3(mouseStartPosition.x, mousePosition.y, 0);
            GL.Vertex3(mousePosition.x, mousePosition.y, 0);
            GL.End();

            //左  
            GL.Begin(GL.LINES);
            GL.Color(clr);
            GL.Vertex3(mouseStartPosition.x, mouseStartPosition.y, 0);
            GL.Vertex3(mouseStartPosition.x, mousePosition.y, 0);
            GL.End();

            //右  
            GL.Begin(GL.LINES);
            GL.Color(clr);
            GL.Vertex3(mousePosition.x, mouseStartPosition.y, 0);
            GL.Vertex3(mousePosition.x, mousePosition.y, 0);
            GL.End();

            GL.PopMatrix(); //还原  
        }
    }
}